package com.opensource.bykc.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.opensource.bykc.entity.GPSPoint;
import com.opensource.bykc.entity.GPSPointDto;
import com.opensource.bykc.entity.PageDto;
import com.opensource.bykc.mapper.GPSPointMapper;
import com.opensource.bykc.service.GPSPointService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestParam;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author 25133
 * @description 针对表【GPSPoint】的数据库操作Service实现
 * @createDate 2024-06-08 14:14:39
 */
@Service
public class GPSPointServiceImpl extends ServiceImpl<GPSPointMapper, GPSPoint>
        implements GPSPointService {

    @Autowired
    private GPSPointMapper mapper;

    @Override
    public boolean add(String lonlat,String name) {
        GPSPoint gpsPoint = new GPSPoint();
        gpsPoint.setShape(lonlat);
        gpsPoint.setName(name);
        gpsPoint.setTime(new Date());
        return mapper.insert(gpsPoint) > 0;

    }

    @Override
    public List<String> find( String start, String end) {
      return   mapper.find(start,end);



    }

    @Override
    public PageDto findPage(String start, String end, Integer curPage, Integer pageSize) {
        LambdaQueryWrapper<GPSPoint> wrapper = new LambdaQueryWrapper<>();
        if (StringUtils.hasText(start)&&StringUtils.hasText(end)){
            // 使用DateTimeFormatter来解析日期字符串
            DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
            wrapper.between(GPSPoint::getTime, LocalDate.parse(start, formatter), LocalDate.parse(end, formatter));
        }
        wrapper.orderByAsc(GPSPoint::getId);
        Page<GPSPoint> page = new Page<>(curPage, pageSize);
        page(page, wrapper);
        List<GPSPoint> records = page.getRecords();
        List<GPSPointDto> collect = records.stream().map(item -> {
            GPSPointDto gpsPointDto = new GPSPointDto();
            BeanUtils.copyProperties(item, gpsPointDto);
            String[] coordinates = item.getShape().split(",");
            gpsPointDto.setLon(coordinates[0]);
            gpsPointDto.setLat(coordinates[1]);
            return gpsPointDto;
        }).collect(Collectors.toList());
        PageDto pageDto = new PageDto(collect, page.getTotal());
        return pageDto;

    }

    @Override
    public boolean edit(GPSPointDto gpsPointDto) {
        GPSPoint gpsPoint = new GPSPoint();
        BeanUtils.copyProperties(gpsPointDto,gpsPoint);
        String shape = gpsPointDto.getLon()+ ','+gpsPointDto.getLat();
        gpsPoint.setShape(shape);
        int i = mapper.updateById(gpsPoint);
        return i>0;
    }

    @Override
    public List<Map<String, Object>> getIn() {
        String shape = "(114.28551,39.976095)";
        return mapper.getIn("('"+shape+"')");



    }
}




